package demo1;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-19
 * Time: 13:34
 */
public class Test {
    //消失的数字
    public static int missingNumber(int[] nums) {
        int sumOrigin = 0;
        int sum = 0;
        int lost = 0;
        int n = nums.length;
        for (int i = 1; i <= n; i++) {
            sumOrigin += i;
            sum += nums[i - 1];
        }
        return sumOrigin - sum;
    }

    public static void rotate1(int[] nums, int k) {
        while ((k--) > 0) {
            int tmp = nums[nums.length - 1];
            int cur = nums.length - 1;
            while (cur > 0) {
                // 交换
                nums[cur] = nums[cur - 1];
                cur--;
            }
            nums[0] = tmp;// 把tmp放回第一个位置
        }
    }

    public static void rotate(int[] nums, int k) {
        //开辟新的空间
        int[] tmp = new int[nums.length];
        k = k % nums.length;
        if (k == 0) {
            return;
        }
        while (k > 0) {
            int last = nums[nums.length - 1];//最后一个数

            for (int j = 1; j <= nums.length - 1; j++) {
                tmp[j] = nums[j - 1];
            }

            tmp[0] = last;
            for (int i = 0; i < nums.length; i++) {
                nums[i] = tmp[i];
            }
            k--;
        }
    }

    public void rotate2(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);
    }

    public void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start += 1;
            end -= 1;
        }
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        rotate(array, 3);
        System.out.println(Arrays.toString(array));
    }


    public static void main1(String[] args) {
        int[] arr = {3, 0, 1};
        int ret = missingNumber(arr);
        System.out.println(ret);
    }
}
